package lionel.MyBrowser;

import java.awt.Container;

import javax.swing.SwingUtilities;

import jmri.util.JmriJFrame;
import lionel.MyController.MyController;
import lionel.MyJMRI.MyTrain.MyTrainsInstance;


//Implement a MyBrowsingCtrl using JmriJFrame display

public class MyBrowserJmriJFrame extends JmriJFrame implements MyBrowsingCtrl {
	private static final long serialVersionUID = -4691813510155262002L;

	static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(MyBrowserJmriJFrame.class.getName());		

	protected MyBrowsableList mLst1;
	protected MyBrowsableList mLst2;
	protected MyBrowsableElement mCurElt;
	protected MyController mController;

	protected MyBrowserJmriJFrame(String name)
	{ 
		super(name);
	}

	public MyBrowserJmriJFrame(MyBrowsableList lst1, MyBrowsableList lst2, MyController controller) {
		super();
		setResizable(false);
		mLst1 = lst1;
		mLst2 = lst2;
		mCurElt = null;
		mController = controller;
		lst1Next();	
	}
	
	public void refresh()
	{ SwingUtilities.invokeLater(new refresh(this));}
	private class refresh implements Runnable
	{
		MyBrowserJmriJFrame mCont;
		public refresh(MyBrowserJmriJFrame cont) { mCont = cont; }
		public void run() {
			mCurElt.setContainer(mCont);
			setContentPane( (Container) mCurElt);
			setPreferredSize( ((Container) mCurElt).getPreferredSize() );
			setTitle( mCurElt.toString()+" by "+mController.getID()  );
			pack();
		}
	}

	public void lst1Next() {
		MyBrowsableElement elt = mLst1.getNext() ;
		if ((elt == mCurElt) || (elt == null))
			elt = mLst1.getNext( ) ;
		if ((elt == mCurElt) || (elt == null))
			return;
		if (mCurElt!=null) mCurElt.setDisplayed(false);
		mCurElt = elt;
		mController.isConductor( mCurElt.getConductor() );
		refresh();
	}

	public void lst1Prev() {
		MyBrowsableElement elt = mLst1.getPrev() ;
		if ((elt == mCurElt) || (elt == null))
			elt = mLst1.getPrev() ;
		if ((elt == mCurElt) || (elt == null))
			return;
		if (mCurElt!=null) mCurElt.setDisplayed(false);	
		mCurElt = elt;
		mController.isConductor( mCurElt.getConductor() );
		refresh();
	}

	public void lst2Next() {
		MyBrowsableElement elt = mLst2.getNext() ;
		if ((elt == mCurElt) || (elt == null))
			elt = mLst2.getNext() ;
		if ((elt == mCurElt) || (elt == null))
			return;
		if (mCurElt!=null) mCurElt.setDisplayed(false);		
		mCurElt = elt;
		if (mCurElt!=null) mCurElt.setDisplayed(true);	
		mController.isConductor( mCurElt.getConductor() );
		refresh();
	}

	public void lst2Prev() {
		MyBrowsableElement elt = mLst2.getPrev() ;
		if ((elt == mCurElt) || (elt == null))
			elt = mLst2.getPrev( ) ;
		if ((elt == mCurElt) || (elt == null))
			return;
		if (mCurElt!=null) mCurElt.setDisplayed(false);		
		mCurElt = elt;
		if (mCurElt!=null) mCurElt.setDisplayed(true);	
		mController.isConductor( mCurElt.getConductor() );
		refresh();
	}

	public void select() { // select/unselect element from list and move to it/from it
		if (mCurElt!=null)
		{
			mCurElt.setDisplayed(false);
			if  ( MyTrainsInstance.getInstance().contains( mCurElt) )
			{  // Removing instance from list 2					
				mCurElt = mLst2.select();
				if (mCurElt != null)
				{
					mCurElt.setDisplayed(true);
					mController.isConductor( mCurElt.getConductor() );
				}
				else
				{ // nothing left in lst2, go on lst1
					lst1Next();
					return;
				}
			}
			else
			{  // adding to lst2
				mCurElt = mLst1.select();
				mLst2.moveCursorTo(mCurElt);
				log.debug("elt has been added to lst2");
				// setting control to new train
				mCurElt.setDisplayed(true);
				mController.isConductor( mCurElt.getConductor() );
			}			
			refresh();
		}
	}

	public String getID() {
		return mController.getID();
	}

}
